package com.jackrain.nea.fc.vp.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.utils.ObjectToUtil;
import com.jackrain.nea.fc.vp.api.ViewCvipopenCmd;
import com.jackrain.nea.fc.vp.mapper.ViewCvipopenMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author xuxinshuai
 * @date 2017/11/13
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "vp")
public class ViewCvipopenCmdImpl extends CommandAdapter implements ViewCvipopenCmd {
    @Reference(version = "2", group = "cp")
    private QueryCmd queryCmd;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        ViewCvipopenMapper viewCvipopenMapper = ApplicationContextHandle.getBean(ViewCvipopenMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if (null == param || param.size() == 0 || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        log.info("ViewCvipopenCmdImpl--param-->{}",param);

//        log.debug("----------VIP查询查询参数-----------" + param);
        List<HashMap> mapList = new ArrayList<>();
        mapList = viewCvipopenMapper.selectvipopensql(param.getJSONArray("IDS").toArray());
        JSONArray jsonArray = listToJsonarray(mapList);
        JSONArray toF4Array = new JSONArray();
//        log.debug("------------开卡返回类型-------------" + mapList);

        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String groupecode = viewCvipopenMapper.selectviptypegroupecode(jsonObject.getLong("VP_C_VIPTYPEGROUP_ID"));

            if ("FF".equals(groupecode)) {

                JSONObject toF4Jo = new JSONObject();
                // VIP等级
                if (jsonObject.getString("VP_C_VIPTYPE_ID") != null) {
                    Long viptypeid = jsonObject.getLong("VP_C_VIPTYPE_ID");
                    String typeecode = viewCvipopenMapper.selectviptypeecode(viptypeid);
                    toF4Jo.put("VP_C_VIPTYPE_ID", typeecode);
                } else {
                    toF4Jo.put("VP_C_VIPTYPE_ID", "");
                }
                //卡号
                if (jsonObject.getString("ECODE") != null) {
                    String openecode = jsonObject.getString("ECODE");
                    toF4Jo.put("ECODE", openecode);
                } else {
                    toF4Jo.put("ECODE", "");
                }
                // 姓名
                String ename = jsonObject.getString("ENAME");
                if (!ObjectToUtil.isNullOREmpty(ename)) {
                    toF4Jo.put("ENAME", ename);
                } else {
                    toF4Jo.put("ENAME", "");
                }
                // 性别
                if (jsonObject.getString("SEX") != null) {
                    String sex = jsonObject.getString("SEX");
                    if ("M".equals(sex)) {
                        toF4Jo.put("SEX", 1);
                    } else if ("W".equals(sex)) {
                        toF4Jo.put("SEX", 2);
                    } else if ("X".equals(sex)) {
                        toF4Jo.put("SEX", 0);
                    }
                } else {
                    toF4Jo.put("SEX", "");
                }
                // 生日-年、月、日
                if (jsonObject.getString("BIRTHDATE") != null) {

                    Date scoreDate = TypeUtils.castToDate(jsonObject.getString("BIRTHDATE"));
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String sc = simpleDateFormat.format(scoreDate);
                    toF4Jo.put("BIRTHDATE", sc);

                } else {
                    toF4Jo.put("BIRTHDATE", "");
                }
                //  手机号码
                if (jsonObject.getString("MOBIL") != null) {
                    toF4Jo.put("MOBIL", jsonObject.getString("MOBIL").trim());
                } else {
                    toF4Jo.put("MOBIL", "");
                }
                // 申请门店
                if (jsonObject.getString("CP_C_STORE_ID") != null) {
                    Long storeid = jsonObject.getLong("CP_C_STORE_ID");
                    HashMap hashMap = new HashMap();
                    hashMap.put("tableName", "CP_C_STORE");
                    hashMap.put("fields", "ECODE");
                    JSONObject whereJo = new JSONObject();
                    whereJo.put("ID", storeid);
                    hashMap.put("whereKeys", whereJo);
                    hashMap.put("type", HashMap.class);
                    HashMap result = (HashMap) queryCmd.queryTable(hashMap);
                    if (result != null) {
                        String storeecode = (String) result.get("ECODE");
                        toF4Jo.put("CP_C_STORE_ID", storeecode);
                    }
                } else {
                    toF4Jo.put("CP_C_STORE_ID", "");
                }
                //  职业
                if (jsonObject.getString("ADDRESS") != null) {
                    toF4Jo.put("ADDRESS", jsonObject.getString("ADDRESS"));
                } else {
                    toF4Jo.put("ADDRESS", "");
                }
                //  职业
                if (jsonObject.getString("JOB") != null) {
                    toF4Jo.put("JOB", jsonObject.getString("JOB"));
                } else {
                    toF4Jo.put("JOB", "");
                }
                //备注
                if (jsonObject.getString("REMARK") != null) {
                    toF4Jo.put("REMARK", jsonObject.getString("REMARK"));
                } else {
                    toF4Jo.put("REMARK", "");
                }
                // 是否接受促销信息
                if (jsonObject.getString("ISRECEIVEPROM") != null) {

                    String isreceiveprom = jsonObject.getString("ISRECEIVEPROM");
                    if ("Y".equals(isreceiveprom)) {
                        toF4Jo.put("ISRECEIVEPROM", 1);
                    } else if ("N".equals(isreceiveprom)) {
                        toF4Jo.put("ISRECEIVEPROM", 0);
                    }

                } else {
                    toF4Jo.put("ISRECEIVEPROM", 0);
                }
                //接收促销信息方式
                if (jsonObject.getString("RECEIVEWAY") != null) {
                    toF4Jo.put("RECEIVEWAY", jsonObject.getString("RECEIVEWAY"));
                } else {
                    toF4Jo.put("RECEIVEWAY", "");
                }
                //微信号
                if (jsonObject.getString("WECHAT") != null) {
                    toF4Jo.put("WECHAT", jsonObject.getString("WECHAT"));
                } else {
                    toF4Jo.put("WECHAT", "");
                }
                //邮箱 VIP_EMAIL
                if (jsonObject.getString("EMAIL") != null) {
                    toF4Jo.put("EMAIL", jsonObject.getString("EMAIL"));
                } else {
                    toF4Jo.put("EMAIL", "");
                }
                if (jsonObject.getString("OPENID") != null) {

                    toF4Jo.put("OPENID", jsonObject.getString("OPENID"));
                } else {
                    toF4Jo.put("OPENID", "");
                }
                if (jsonObject.getString("ID_CARD") != null) {
                    toF4Jo.put("ID_CARD", jsonObject.getString("ID_CARD"));
                } else {
                    toF4Jo.put("ID_CARD", "");
                }
                if (jsonObject.getString("ACADEMIC") != null) {
                    toF4Jo.put("ACADEMIC", jsonObject.getString("ACADEMIC "));
                } else {
                    toF4Jo.put("ACADEMIC", "");
                }
                if (jsonObject.getString("ISACTIVE") != null) {
                    String isactive = jsonObject.getString("ISACTIVE");
                    if ("Y".equals(isactive)) {
                        toF4Jo.put("ISACTIVE", 1);
                    } else if ("N".equals(isactive)) {
                        toF4Jo.put("ISACTIVE", 2);
                    }
                } else {
                    toF4Jo.put("ISACTIVE", "");
                }
                String source = jsonObject.getString("SOURCE");
                if (!ObjectToUtil.isNullOREmpty(source)) {
                    toF4Jo.put("SOURCE", jsonObject.getString("SOURCE"));
                } else {
                    toF4Jo.put("SOURCE", "");
                }
                toF4Array.add(toF4Jo);
            }
        }
        vh.put("data", toF4Array);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("success"));
        return vh;
    }

    /**
     * list转jsonarray
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }
}
